package DAO;

import Damain.emp;
import Utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EmpUpdateUiDaoImpl implements EmpUpdateUiDao {

    /**
     *
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public emp SelEmpByID(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        connection = JdbcUtils.getConnection();
        String sql = "SELECT * FROM t_emp where id = ?";
        emp emp = new emp();
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                //封装数据

                emp.setId(resultSet.getInt("id"));
                emp.setEname(resultSet.getString("ename"));
                emp.setAge(resultSet.getInt("age"));
                emp.setSex(resultSet.getString("sex"));
                emp.setSal(resultSet.getInt("sal"));
                emp.setBirthday(resultSet.getString("birthday"));
                emp.setEdate(resultSet.getString("edate"));

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(connection, statement, resultSet);
        }
        return emp;
    }
}
